<template>
    <div class="home-wrapper">
        <div class="content">
            <transition>
                <router-view></router-view>
            </transition>
        </div>
        <div class="menu-wrapper">
            <ul class="menu">
                <li ref="tabItem" v-for="(item, index) in menuList" :key="index" @click="change(index)">
                    <div class="title">{{item.title}}</div>
                </li>
                <div class="slider" :style="{ width: tabWidth+'px', left: tabWidth * activeMenu + 'px'}"></div>
            </ul>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            counter: 0,
            tabWidth: 0,
            tabHeight: 0,
            activeMenu: 0,
            menuList: [{
                    title: "进行中...",
                    page: "code",
                },
                {
                    title: "饼及环",
                    page: "pie",
                },
                {
                    title: "柱状图",
                    page: "his",
                },
                {
                    title: "折线图",
                    page: "line",
                },
                {
                    title: "面积图",
                    page: "area",
                },
                {
                    title: "散点图",
                    page: "splashes",
                },
                {
                    title: "金字塔图",
                    page: "pyramid",
                },
                {
                    title: "其他...",
                    page: "other",
                },
            ],
        };
    },
    mounted() {
        const route = this.$route.path;
        const index = this.menuList.findIndex((item) => {
            const curPage = `/${item.page}`;
            return curPage === route;
        });
        this.activeMenu = index;
        this.getTabWidth();
    },
    methods: {
        getTabWidth() {
            const dom = this.$refs.tabItem[0];
            const observer = new ResizeObserver((e) => {
                const { width, height } = e[0].contentRect;
                this.tabWidth = width;
                this.tabHeight = height;
            });
            observer.observe(dom);
        },
        change(index) {
            if (this.activeMenu === index) {
                return;
            }
            this.activeMenu = index;
            this.$router.push({ name: this.menuList[index].page });
        },
    },
};
</script>

<style lang="scss">
div::-webkit-scrollbar {
    width: 0;
}

.home-wrapper {
    user-select: none;
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;
    .content {
        flex: 1;
        overflow: hidden;
        overflow-y: auto;
    }
    .menu-wrapper {
        margin: 20px 20px 0 20px;
    }
    .menu {
        width: 100%;
        height: 100%;
        border-radius: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #fff;
        list-style: none;
        margin: 0;
        padding: 0;
        background: rgba(74, 84, 100, 1);
        position: relative;
        li:first-child {
            border-top-left-radius: 10px;
            border-bottom-left-radius: 10px;
        }
        li:last-child {
            border-top-right-radius: 10px;
            border-bottom-right-radius: 10px;
        }
        li {
            cursor: pointer;
            flex: 1;
            width: 100%;
            height: 80px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: rgba(74, 84, 100, 0);
            position: relative;
        }
    }
    .slider {
        width: 200px;
        height: 80px;
        background: salmon;
        position: absolute;
        border-radius: 10px;
        left: 0;
        top: 0;
        transition: left 300ms;
    }
    .title {
        position: absolute;
        z-index: 2;
    }
    .fix-btn {
        cursor: pointer;
        position: fixed;
        width: 50px;
        height: 50px;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        right: 300px;
        top: 100px;
        font-size: 40px;
        background: #fff;
        color: rgba(74, 84, 100, 1);
        font-weight: bolder;
        transition: all 150ms;
    }
    .fix-btn:hover {
        background: salmon;
        color: #fff;
        transform: rotate(90deg);
        box-shadow: 1px 1px 10px 4px #888888;
    }
    .fix-btn:leave {
        transform: rotate(-90deg);
    }
}
</style>
